package com.lang.actions;

import org.apache.struts2.ServletActionContext;

import com.lang.beans.Courier;
import com.lang.beans.User;
import com.lang.service.CourierService;
import com.lang.service.UserService;
import com.lang.util.Config;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
/**
 * 登录Action
 * @author Lang
 *
 */
public class LoginAction implements Action {
    
	private Courier courier;   //邮差
	private User user;   //用户
	private String id;         //获取界面邮递员输入的员工号
    private String username;   //获取用户输入的用户名
    private String password;   //获取邮递员或用户输入的密码
    private CourierService courierservice;  //邮差服务类，提供邮差登录服务
    private UserService userservice;   //用户服务类，提供用户登录服务


    public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
    public String getUsername() {
        return username;
    }

	public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

	public Courier getCourier() {
		return courier;
	}

	public void setCourier(Courier courier) {
		this.courier = courier;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public CourierService getCourierservice() {
		return courierservice;
	}

	public void setCourierservice(CourierService courierservice) {
		this.courierservice = courierservice;
	}

	public UserService getUserservice() {
		return userservice;
	}

	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}

	/**
	 * 邮差登录处理函数
	 * @return
	 * @throws Exception
	 * @author Lang
	 */
	public String curiersLogin() throws Exception {
		courier=courierservice.loginservice(getId(), getPassword());
        if (courier!=null) {
        	if(courier.getPermission()==Config.COURIER){   //如果是普通邮差，跳转到普通邮差页面
        		ServletActionContext.getRequest().getSession().setAttribute("courier", courier);
//        		ServletActionContext.getRequest().getSession().setAttribute("cid", courier.getId());
                return "courier";
        	}else{     //管理员则跳转到管理员界面
        		ServletActionContext.getRequest().getSession().setAttribute("admin", courier);   
        		return "admin";
        	}
        }
        return "error";  //密码或用户名错误
	}
	/**
	 * 普通用户登录处理函数
	 * @return
	 * @throws Exception
	 */
	public String userLogin() throws Exception {
		user=userservice.loginservice(getUsername(), getPassword());
        if (user!=null){
           ServletActionContext.getRequest().getSession().setAttribute("user", user);
           ServletActionContext.getRequest().getSession().setAttribute("uid", user.getId());
           return "success";
        }
        return "error";
	}
	

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}